﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;
using HYCreateDuct.MethodCreateDuct;
using HYCreateDuct.MethodHelp;
using HYCreateDuct.Properties;

namespace HYCreateDuct.FormDuct
{
	public partial class FormCreateDuct : HYBaseForm
	{
		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public FormCreateDuct(UIApplication m_uiApp, string m_title)
		{
			this.InitializeComponent();
			this.uiApp = m_uiApp;
			this.doc = m_uiApp.ActiveUIDocument.Document;
			this.title = m_title;
			this.Text = m_title;
			if (this.title.Equals("绘制风管"))
			{
				this.groupBoxS.Visible = false;
				this.groupBoxH1.Visible = true;
				this.groupBoxH2.Visible = true;
			}
			else
			{
				this.groupBoxH1.Visible = false;
				this.groupBoxH2.Visible = false;
				this.groupBoxS.Visible = true;
			}
			this.InitWindow(null, false);
		}

		public FormCreateDuct(UIApplication uiApp, string m_title, bool bl = true)
		{
			this.InitializeComponent();
			this.uiApp = uiApp;
			this.Text = m_title;
			this.doc = uiApp.ActiveUIDocument.Document;
		}

		private void InitWindow(List<DuctSytemSet> lstCurrentOut = null, bool bl = false)
		{
			DuctHis history = new DuctHisHelp().GetHistory();
			this.nInner = history.nInner;
			this.nTypeSelect = history.nTypeSelect;
			this.tbFlow.Text = history.nFlow.ToString();
			this.tbLevelOffset.Text = history.nOffset.ToString();
			this.cbSystemType.Items.Clear();
			if (!bl)
			{
				this.cbDuctType.Items.Clear();
			}
			List<string> elementNameByClass = YJKElementFilter.GetElementNameByClass(typeof(MechanicalSystemType), this.doc);
			if (lstCurrentOut != null)
			{
				elementNameByClass.Clear();
				foreach (DuctSytemSet ductSytemSet in lstCurrentOut)
				{
					elementNameByClass.Add(ductSytemSet.strSystemName);
				}
			}
			if (this.nInner != 0)
			{
				elementNameByClass.Clear();
				List<DuctSystemList> list = new XmlHelp().ReadSystemSet("");
				if (list.Count <= this.nTypeSelect)
				{
					this.nTypeSelect = 0;
				}
				foreach (DuctSytemSet ductSytemSet2 in list[this.nTypeSelect].lstSet)
				{
					elementNameByClass.Add(ductSytemSet2.strSystemName);
				}
				this.lstOutDuctSytemSet = list[this.nTypeSelect].lstSet;
			}
			foreach (string item in elementNameByClass)
			{
				this.cbSystemType.Items.Add(item);
			}
			if (elementNameByClass.Count > history.nSystemType)
			{
				this.cbSystemType.SelectedIndex = history.nSystemType;
			}
			else
			{
				this.cbSystemType.SelectedIndex = 0;
			}
			this.initDuctType(history, bl);
			this.txtWidth.Text = history.nWidth.ToString();
			this.txtHeight.Text = history.nHeight.ToString();
			this.ButtonDuiqi(history.nType);
			this.Duiqi = history.nType;
			this.tbHorizontalOffset.Text = history.nTypeOffset.ToString();
			if (history.xdLevel == 0)
			{
				this.rbxdLevel.Checked = true;
			}
			else
			{
				this.rbjdLevel.Checked = true;
			}
			SqlLiteHelp.SqlLiteDatabase(Path.Combine(Product.DataLocation, "HYBIMSPACE.db3"));
			double flow = Convert.ToDouble(this.tbFlow.Text);
			int num = Convert.ToInt32(this.txtWidth.Text);
			if (this.cbDuctType.Text.Contains("矩形") || this.cbDuctType.Text.Contains("椭圆形"))
			{
				this.txtHeight.Visible = true;
				int num2 = Convert.ToInt32(this.txtHeight.Text);
				this.InitialListView(this.GetRectangeSec(0), this.lvWidth, "宽(W)", num2);
				this.InitialListView(this.GetRectangeSec(1), this.lvHeight, "高(H)", num);
				double num3 = this.CalcRecDuctSpeed(flow, num, num2);
				double num4 = this.CalcRpa((double)num, (double)num2, num3);
				this.lblSpeed.Text = "V(m/s) = " + num3;
				this.lbRPa.Text = "R(Pa/m) =" + num4;
				List<System.Drawing.Color> color = SqlLiteHelp.GetColor(num3, num4);
				this.lblSpeed.ForeColor = color[0];
				this.lbRPa.ForeColor = color[1];
				this.lvHeight.Visible = true;
				this.btnChange.Visible = true;
				this.SelectViewItem(this.lvWidth, num);
				this.SelectViewItem(this.lvHeight, num2);
			}
			else
			{
				this.txtHeight.Visible = false;
				double num5 = this.CalcRouDuctSpeed(flow, num);
				double num6 = this.CalcRpa((double)num, 0.0, num5);
				this.lblSpeed.Text = "V(m/s) = " + num5;
				this.lbRPa.Text = "R(Pa/m) =" + num6;
				List<System.Drawing.Color> color2 = SqlLiteHelp.GetColor(num5, num6);
				this.lblSpeed.ForeColor = color2[0];
				this.lbRPa.ForeColor = color2[1];
				this.InitialListView(this.GetRoundSec(), this.lvWidth, "直径", -1);
				this.lvHeight.Visible = false;
				this.btnChange.Visible = false;
				this.SelectViewItem(this.lvWidth, num);
			}
			this.cbLevel.Items.Clear();
			foreach (string item2 in YJKElementFilter.GetElementNameByClass(typeof(Level), this.doc))
			{
				this.cbLevel.Items.Add(item2);
				this.cbStartLevel.Items.Add(item2);
				this.cbEndLevel.Items.Add(item2);
			}
			if (this.doc.ActiveView.GenLevel != null)
			{
				this.cbLevel.Text = this.doc.ActiveView.GenLevel.Name;
				this.cbStartLevel.Text = this.doc.ActiveView.GenLevel.Name;
				this.cbEndLevel.Text = this.doc.ActiveView.GenLevel.Name;
			}
			else
			{
				this.cbLevel.SelectedIndex = 0;
				this.cbStartLevel.SelectedIndex = 0;
				this.cbEndLevel.SelectedIndex = 0;
			}
			this.InitOffset(history);
		}

		private void btnChange_Click(object sender, EventArgs e)
		{
			int num = Convert.ToInt32(this.txtWidth.Text);
			int num2 = Convert.ToInt32(this.txtHeight.Text);
			this.txtWidth.Text = num2.ToString();
			this.txtHeight.Text = num.ToString();
		}

		private void cbDuctType_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (!string.IsNullOrEmpty(this.cbSystemType.Text))
			{
				this.ComBoxChangeBanding();
			}
		}

		private void initDuctType(DuctHis hisHelpH, bool bl)
		{
			if (bl)
			{
				return;
			}
			this.cbDuctType.Items.Clear();
			List<Element> elementByClass = YJKElementFilter.GetElementByClass(typeof(DuctType), this.doc);
			elementByClass.Reverse();
			int num = 0;
			foreach (Element element in elementByClass)
			{
				DuctType ductType = element as DuctType;
				this.cbDuctType.Items.Add(ductType.FamilyName + "-" + ductType.Name);
				ductType.FamilyName.Contains("矩形");
				num++;
			}
			if (this.cbDuctType.Items.Count > hisHelpH.nDuctType)
			{
				this.cbDuctType.SelectedIndex = hisHelpH.nDuctType;
				return;
			}
			if (this.cbDuctType.Items.Count > 0)
			{
				this.cbDuctType.SelectedIndex = 0;
			}
		}

		private void lvWidth_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.lvWidth.SelectedItems.Count > 0)
			{
				string text = this.lvWidth.SelectedItems[0].SubItems[0].Text;
				this.txtWidth.Text = text;
				System.Drawing.Color backColor = this.lvWidth.BackColor;
				System.Drawing.Color lightSkyBlue = System.Drawing.Color.LightSkyBlue;
				foreach (object obj in this.lvWidth.Items)
				{
					ListViewItem listViewItem = (ListViewItem)obj;
					listViewItem.BackColor = backColor;
					if (listViewItem.Index == this.lvWidth.SelectedItems[0].Index)
					{
						listViewItem.BackColor = lightSkyBlue;
					}
				}
			}
		}

		private void lvHeight_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.lvHeight.SelectedItems.Count > 0)
			{
				string text = this.lvHeight.SelectedItems[0].SubItems[0].Text;
				this.txtHeight.Text = text;
				System.Drawing.Color backColor = this.lvWidth.BackColor;
				System.Drawing.Color lightSkyBlue = System.Drawing.Color.LightSkyBlue;
				foreach (object obj in this.lvHeight.Items)
				{
					ListViewItem listViewItem = (ListViewItem)obj;
					listViewItem.BackColor = backColor;
					if (listViewItem.Index == this.lvHeight.SelectedItems[0].Index)
					{
						listViewItem.BackColor = lightSkyBlue;
					}
				}
			}
		}

		private void txtWidth_TextChanged(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(this.txtWidth.Text) || this.txtWidth.Text.Equals("0"))
			{
				this.txtWidth.Text = "";
				return;
			}
			try
			{
				double flow = Convert.ToDouble(this.tbFlow.Text);
				int num = Convert.ToInt32(this.txtWidth.Text);
				string text = this.cbDuctType.Text;
				SqlLiteHelp.SqlLiteDatabase(Path.Combine(Product.DataLocation, "HYBIMSPACE.db3"));
				if (text.Contains("矩形") || text.Contains("椭圆形"))
				{
					int num2 = Convert.ToInt32(this.txtHeight.Text);
					this.InitialListView(this.GetRectangeSec(1), this.lvHeight, "高(H)", num);
					double num3 = this.CalcRecDuctSpeed(flow, num, num2);
					double num4 = this.CalcRpa((double)num, (double)num2, num3);
					this.lblSpeed.Text = "V(m/s) = " + num3;
					this.lbRPa.Text = "R(Pa/m) =" + num4;
					List<System.Drawing.Color> color = SqlLiteHelp.GetColor(num3, num4);
					this.lblSpeed.ForeColor = color[0];
					this.lbRPa.ForeColor = color[1];
					this.SelectViewItem(this.lvHeight, num2);
					this.SelectViewItem(this.lvWidth, num);
				}
				else
				{
					double num5 = this.CalcRouDuctSpeed(flow, num);
					double num6 = this.CalcRpa((double)num, 0.0, num5);
					this.lblSpeed.Text = "V(m/s) =" + num5;
					this.lbRPa.Text = "R(Pa/m) =" + num6;
					List<System.Drawing.Color> color2 = SqlLiteHelp.GetColor(num5, num6);
					this.lblSpeed.ForeColor = color2[0];
					this.lbRPa.ForeColor = color2[1];
					this.SelectViewItem(this.lvWidth, num);
				}
			}
			catch
			{
				this.txtWidth.Text = "";
			}
		}

		private void txtHeight_TextChanged(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(this.txtHeight.Text) || this.txtHeight.Text.Equals("0"))
			{
				this.txtHeight.Text = "";
				return;
			}
			try
			{
				double flow = Convert.ToDouble(this.tbFlow.Text);
				int num = Convert.ToInt32(this.txtWidth.Text);
				int num2 = Convert.ToInt32(this.txtHeight.Text);
				string text = this.cbDuctType.Text;
				SqlLiteHelp.SqlLiteDatabase(Path.Combine(Product.DataLocation, "HYBIMSPACE.db3"));
				if (text.Contains("矩形") || text.Contains("椭圆形"))
				{
					this.InitialListView(this.GetRectangeSec(1), this.lvWidth, "宽(W)", num2);
					double num3 = this.CalcRecDuctSpeed(flow, num, num2);
					double num4 = this.CalcRpa((double)num, (double)num2, num3);
					this.lblSpeed.Text = "V(m/s) = " + num3;
					this.lbRPa.Text = "R(Pa/m) =" + num4;
					List<System.Drawing.Color> color = SqlLiteHelp.GetColor(num3, num4);
					this.lblSpeed.ForeColor = color[0];
					this.lbRPa.ForeColor = color[1];
					this.SelectViewItem(this.lvWidth, num);
					this.SelectViewItem(this.lvHeight, num2);
				}
			}
			catch
			{
				this.txtHeight.Text = "";
			}
		}

		private void tbFlow_TextChanged(object sender, EventArgs e)
		{
			try
			{
				if (Convert.ToDouble(this.tbFlow.Text) > 2.0)
				{
					this.ComBoxChangeBanding();
				}
			}
			catch
			{
				this.tbFlow.Text = "";
			}
		}

		private void btnExtract_Click(object sender, EventArgs e)
		{
			this.blTiqvDuct = true;
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			if (this.title.Equals("绘制风管"))
			{
				FormCreateDuct.SendMessage(cmdCreateDuct.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			}
			else
			{
				FormCreateDuct.SendMessage(cmdCreateVerticalDuct.hWndRevitS.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			}
			FormCreateDuct.keybd_event((byte)bVk, 0, dwFlags, 0);
			FormCreateDuct.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void btnSet_Click(object sender, EventArgs e)
		{
			DuctHisHelp ductHisHelp = new DuctHisHelp();
			FormDuctSet formDuctSet = new FormDuctSet(this.uiApp, this.nInner, this.nTypeSelect, ductHisHelp);
			DialogResult dialogResult = formDuctSet.ShowDialog();
			if (dialogResult == DialogResult.OK)
			{
				this.nInner = formDuctSet.nInner;
				this.nTypeSelect = formDuctSet.nTypeSelect;
				ductHisHelp.WriteHistory(this.nInner, this.nTypeSelect);
				if (!base.ClientRectangle.Contains(Cursor.Position))
				{
					int wMsg = 7;
					if (this.title.Equals("绘制风管"))
					{
						FormCreateDuct.SendMessage(cmdCreateDuct.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
					}
					else
					{
						FormCreateDuct.SendMessage(cmdCreateVerticalDuct.hWndRevitS.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
					}
				}
				else
				{
					base.Focus();
				}
				if (formDuctSet.lstCurrentOut.Count > 0)
				{
					this.InitWindow(formDuctSet.lstCurrentOut, true);
					this.lstOutDuctSytemSet = formDuctSet.lstCurrentOut;
				}
				else
				{
					this.InitWindow(null, true);
				}
			}
			if (dialogResult == DialogResult.Cancel)
			{
				this.nInner = formDuctSet.nInner;
				this.nTypeSelect = formDuctSet.nTypeSelect;
				ductHisHelp.WriteHistory(this.nInner, this.nTypeSelect);
				if (!base.ClientRectangle.Contains(Cursor.Position))
				{
					int wMsg2 = 7;
					if (this.title.Equals("绘制风管"))
					{
						FormCreateDuct.SendMessage(cmdCreateDuct.hWndRevit.Handle, wMsg2, IntPtr.Zero, IntPtr.Zero);
					}
					else
					{
						FormCreateDuct.SendMessage(cmdCreateVerticalDuct.hWndRevitS.Handle, wMsg2, IntPtr.Zero, IntPtr.Zero);
					}
				}
				else
				{
					base.Focus();
				}
				if (formDuctSet.lstCurrentOut.Count > 0)
				{
					this.InitWindow(formDuctSet.lstCurrentOut, true);
					this.lstOutDuctSytemSet = formDuctSet.lstCurrentOut;
					return;
				}
				this.InitWindow(null, true);
			}
		}

		private void ComBoxChangeBanding()
		{
			if (string.IsNullOrEmpty(this.txtWidth.Text) || string.IsNullOrEmpty(this.tbFlow.Text) || string.IsNullOrEmpty(this.txtHeight.Text))
			{
				return;
			}
			double flow = Convert.ToDouble(this.tbFlow.Text);
			int num = Convert.ToInt32(this.txtWidth.Text);
			string text = this.cbDuctType.Text;
			SqlLiteHelp.SqlLiteDatabase(Path.Combine(Product.DataLocation, "HYBIMSPACE.db3"));
			if (text.Contains("矩形") || text.Contains("椭圆形"))
			{
				this.txtHeight.Visible = true;
				int num2 = Convert.ToInt32(this.txtHeight.Text);
				this.InitialListView(this.GetRectangeSec(0), this.lvWidth, "宽(W)", num2);
				this.InitialListView(this.GetRectangeSec(1), this.lvHeight, "高(H)", num);
				double num3 = this.CalcRecDuctSpeed(flow, num, num2);
				double num4 = this.CalcRpa((double)num, (double)num2, num3);
				this.lblSpeed.Text = "V(m/s) = " + num3;
				this.lbRPa.Text = "R(Pa/m) =" + num4;
				List<System.Drawing.Color> color = SqlLiteHelp.GetColor(num3, num4);
				this.lblSpeed.ForeColor = color[0];
				this.lbRPa.ForeColor = color[1];
				this.lvHeight.Visible = true;
				this.btnChange.Visible = true;
				this.SelectViewItem(this.lvWidth, num);
				this.SelectViewItem(this.lvHeight, num2);
				return;
			}
			this.txtHeight.Visible = false;
			double num5 = this.CalcRouDuctSpeed(flow, num);
			double num6 = this.CalcRpa((double)num, 0.0, num5);
			this.lblSpeed.Text = "V(m/s) = " + num5;
			this.lbRPa.Text = "R(Pa/m) =" + num6;
			List<System.Drawing.Color> color2 = SqlLiteHelp.GetColor(num5, num6);
			this.lblSpeed.ForeColor = color2[0];
			this.lbRPa.ForeColor = color2[1];
			this.InitialListView(this.GetRoundSec(), this.lvWidth, "直径", -1);
			this.lvHeight.Visible = false;
			this.btnChange.Visible = false;
			this.SelectViewItem(this.lvWidth, num);
		}

		private void SelectViewItem(ListView lvView, int txtValue)
		{
			System.Drawing.Color backColor = lvView.BackColor;
			System.Drawing.Color lightSkyBlue = System.Drawing.Color.LightSkyBlue;
			for (int i = 0; i < lvView.Items.Count; i++)
			{
				ListViewItem listViewItem = lvView.Items[i];
				listViewItem.BackColor = backColor;
				if (Convert.ToInt32(listViewItem.SubItems[0].Text) == txtValue)
				{
					lvView.Items[i].Selected = true;
					lvView.Items[i].EnsureVisible();
					lvView.Items[i].Focused = true;
					lvView.Items[i].BackColor = lightSkyBlue;
				}
			}
		}

		private void InitialListView(List<int> secList, ListView listView, string colNum, int whValue)
		{
			listView.Items.Clear();
			listView.Columns.Clear();
			double flow = Convert.ToDouble(this.tbFlow.Text);
			this.InitialLVColumn(ref listView, colNum);
			if (whValue < 0)
			{
				using (List<int>.Enumerator enumerator = secList.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						int diameter = enumerator.Current;
						ListViewItem listViewItem = new ListViewItem();
						listViewItem.SubItems.Clear();
						listViewItem.SubItems[0].Text = diameter.ToString();
						double num = this.CalcRouDuctSpeed(flow, diameter);
						listViewItem.SubItems.Add(num.ToString());
						listView.Items.Add(listViewItem);
					}
					return;
				}
			}
			foreach (int width in secList)
			{
				ListViewItem listViewItem2 = new ListViewItem();
				listViewItem2.SubItems.Clear();
				listViewItem2.SubItems[0].Text = width.ToString();
				double num2 = this.CalcRecDuctSpeed(flow, width, whValue);
				listViewItem2.SubItems.Add(num2.ToString());
				listView.Items.Add(listViewItem2);
			}
		}

		private double CalcRecDuctSpeed(double flow, int width, int height)
		{
			return Convert.ToDouble(StringTools.CutOffEfficacious((flow / 3600.0 / ((double)width / 1000.0 * (double)height / 1000.0)).ToString(), 2));
		}

		private double CalcRouDuctSpeed(double flow, int diameter)
		{
			flow /= 3600.0;
			double num = (double)diameter / 1000.0;
			return Convert.ToDouble(StringTools.CutOffEfficacious((flow / (Math.PI * num * num / 4.0)).ToString(), 2));
		}

		private double CalcRpa(double width, double height, double fensu)
		{
			SqlLiteHelp.SqlLiteDatabase(Path.Combine(Product.DataLocation, "HYBIMSPACE.db3"));
			DuctParam ductParam = SqlLiteHelp.ReadDuctParam();
			List<MaterialAndRough> materialAndRough = SqlLiteHelp.GetMaterialAndRough();
			double num;
			if (height == 0.0)
			{
				num = width;
			}
			else
			{
				num = 2.0 * width * height / (width + height);
			}
			num /= 1000.0;
			double num2 = num * fensu / (ductParam.dViscosity * Math.Pow(10.0, -6.0));
			double num3 = Math.Pow(materialAndRough[0].dSetValue / 1000.0 / num + 68.0 / num2, 0.25) * 0.11;
			if (num3 < 0.018)
			{
				num3 = num3 * 0.85 + 0.0028;
			}
			return Math.Round(num3 / num * (fensu * fensu / 2.0) * ductParam.dDensity, 2);
		}

		private void InitialLVColumn(ref ListView lv, string colNum)
		{
			lv.GridLines = true;
			lv.FullRowSelect = true;
            lv.View = System.Windows.Forms.View.Details;
			lv.Scrollable = true;
			lv.MultiSelect = false;
			lv.HeaderStyle = ColumnHeaderStyle.Nonclickable;
			lv.Columns.Add(colNum, 50, HorizontalAlignment.Left);
			lv.Columns.Add("V(m/s)", 50, HorizontalAlignment.Left);
			lv.Visible = true;
		}

		private List<int> GetRectangeSec(int type)
		{
			SqlLiteHelp.SqlLiteDatabase(Path.Combine(Product.DataLocation, "HYBIMSPACE.db3"));
			List<int> list = new List<int>();
			foreach (DuctRectSize ductRectSize in SqlLiteHelp.GetRectSize())
			{
				if (type == 0)
				{
					list.Add(int.Parse(ductRectSize.dWidth.ToString()));
				}
				else
				{
					list.Add(int.Parse(ductRectSize.dHeight.ToString()));
				}
			}
			return this.GetListInt(list);
		}

		private List<int> GetRoundSec()
		{
			SqlLiteHelp.SqlLiteDatabase(Path.Combine(Product.DataLocation, "HYBIMSPACE.db3"));
			List<int> list = new List<int>();
			foreach (double num in SqlLiteHelp.GetCircleSize())
			{
				list.Add(int.Parse(num.ToString()));
			}
			return this.GetListInt(list);
		}

		private List<int> GetListInt(List<int> lstInt)
		{
			for (int i = 0; i < lstInt.Count; i++)
			{
				for (int j = lstInt.Count - 1; j > i; j--)
				{
					if (lstInt[i] == lstInt[j])
					{
						lstInt.RemoveAt(j);
					}
				}
			}
			lstInt.Sort();
			return lstInt;
		}

		private void btnAddMuti_Click(object sender, EventArgs e)
		{
			double num = double.Parse(this.tbHorizontalOffset.Text);
			if (num > 0.0)
			{
				this.tbHorizontalOffset.Text = (num * -1.0).ToString();
				return;
			}
			this.tbHorizontalOffset.Text = (num * -1.0).ToString();
		}

		private void FormCreateDuct_FormClosed(object sender, FormClosedEventArgs e)
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			if (this.title.Equals("绘制风管"))
			{
				FormCreateDuct.SendMessage(cmdCreateDuct.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			}
			else
			{
				FormCreateDuct.SendMessage(cmdCreateVerticalDuct.hWndRevitS.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			}
			FormCreateDuct.keybd_event((byte)bVk, 0, dwFlags, 0);
			FormCreateDuct.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void FormCreateDuct_Leave(object sender, EventArgs e)
		{
			if (base.ClientRectangle.Contains(Cursor.Position))
			{
				base.Focus();
				return;
			}
			int wMsg = 7;
			if (this.title.Equals("绘制风管"))
			{
				FormCreateDuct.SendMessage(cmdCreateDuct.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			FormCreateDuct.SendMessage(cmdCreateVerticalDuct.hWndRevitS.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
		}

		public void FormCreateDuct_MouseLeave(object sender, EventArgs e)
		{
			this.GetStatus();
			if (base.ClientRectangle.Contains(Cursor.Position))
			{
				base.Focus();
				return;
			}
			int wMsg = 7;
			if (this.title.Equals("绘制风管"))
			{
				FormCreateDuct.SendMessage(cmdCreateDuct.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			FormCreateDuct.SendMessage(cmdCreateVerticalDuct.hWndRevitS.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
		}

		public DuctWindowStruct GetData(bool bl = true)
		{
			DuctWindowStruct ductWindowStruct = new DuctWindowStruct();
			try
			{
				ductWindowStruct.strSystemType = this.cbSystemType.Text;
				ductWindowStruct.strDuctType = this.cbDuctType.Text;
				ductWindowStruct.dDuctFlow = double.Parse(this.tbFlow.Text);
				ductWindowStruct.dDuctHeight = double.Parse(this.txtHeight.Text);
				string text = this.cbDuctType.Text;
				if (!text.Contains("矩形") && !text.Contains("椭圆形"))
				{
					ductWindowStruct.dDuctHeight = 0.0;
				}
				ductWindowStruct.dDuctWidth = double.Parse(this.txtWidth.Text);
				ductWindowStruct.dDuctDiameter = double.Parse(this.txtWidth.Text);
				ductWindowStruct.strLevel = this.cbLevel.Text;
				ductWindowStruct.dOffset = double.Parse(this.tbLevelOffset.Text);
				ductWindowStruct.nStyle = this.tempType;
				ductWindowStruct.nHorizontalOffset = double.Parse(this.tbHorizontalOffset.Text);
				ductWindowStruct.nInnerType = this.nInner;
				string s = this.lbRPa.Text.Replace("R(Pa/m) =", "");
				ductWindowStruct.dBimozu = double.Parse(s);
				if (this.title.Equals("绘制竖风管"))
				{
					ductWindowStruct.strLevel = this.cbStartLevel.Text;
					ductWindowStruct.dOffset = double.Parse(this.cbStartOffset.Text);
					if (!this.rbxdLevel.Checked)
					{
						ductWindowStruct.dSOffset = double.Parse(this.cbStartOffset.Text) / 304.8;
						ductWindowStruct.dEOffset = double.Parse(this.cbEndOffset.Text) / 304.8;
					}
					else
					{
						ductWindowStruct.dSOffset = this.zCoord(double.Parse(this.cbStartOffset.Text)) / 1000.0 + this.getLevelOffset(this.cbStartLevel.Text);
						ductWindowStruct.dEOffset = this.zCoord(double.Parse(this.cbEndOffset.Text)) / 1000.0 + this.getLevelOffset(this.cbEndLevel.Text);
					}
				}
			}
			catch
			{
				if (!bl)
				{
					return null;
				}
				return ductWindowStruct;
			}
			return ductWindowStruct;
		}

		private double zCoord(double offset)
		{
			return 0.0 + offset / 0.3048;
		}

		public double getLevelOffset(string level)
		{
			double result = 0.0;
			if (string.IsNullOrEmpty(level))
			{
				return result;
			}
			return this.Get_Level(this.doc, level).ProjectElevation;
		}

		public Level Get_Level(Autodesk.Revit.DB.Document document, string pipingSystemTypeName)
		{
			ICollection<Element> elementByClass = YJKElementFilter.GetElementByClass(typeof(Level), document);
			return this.GetElementSameName(elementByClass, pipingSystemTypeName) as Level;
		}

		private Element GetElementSameName(ICollection<Element> collection, string elemName)
		{
			Element result = null;
			foreach (Element element in collection)
			{
				if (element != null && element.Name == elemName)
				{
					result = element;
				}
			}
			return result;
		}

		private void btnDuiqi_Click(object sender, EventArgs e)
		{
			FormAlign formAlign = new FormAlign();
			int num = (int)formAlign.ShowDialog();
			int duiqi = this.Duiqi;
			double num2 = double.Parse(this.txtHeight.Text);
			string text = this.cbDuctType.Text;
			if (!text.Contains("矩形") && !text.Contains("椭圆形"))
			{
				num2 = 0.0;
			}
			double num3 = double.Parse(this.txtWidth.Text);
			double num4 = double.Parse(this.tbLevelOffset.Text);
			if (duiqi == 7 || duiqi == 8 || duiqi == 9)
			{
				if (num2 != 0.0)
				{
					num4 += num2 / 2.0;
				}
				else
				{
					num4 += num3 / 2.0;
				}
			}
			if (duiqi == 1 || duiqi == 2 || duiqi == 3)
			{
				if (num2 != 0.0)
				{
					num4 -= num2 / 2.0;
				}
				else
				{
					num4 -= num3 / 2.0;
				}
			}
			if (num == 1)
			{
				this.ButtonDuiqi(formAlign.nAlign);
			}
			this.Duiqi = formAlign.nAlign;
			double num5 = 0.0;
			if (this.Duiqi == 4 || this.Duiqi == 5 || this.Duiqi == 6)
			{
				num5 = num4;
			}
			if (this.Duiqi == 7 || this.Duiqi == 8 || this.Duiqi == 9)
			{
				if (num2 != 0.0)
				{
					num5 = num4 - num2 / 2.0;
				}
				else
				{
					num5 = num4 - num3 / 2.0;
				}
			}
			if (this.Duiqi == 1 || this.Duiqi == 2 || this.Duiqi == 3)
			{
				if (num2 != 0.0)
				{
					num5 = num4 + num2 / 2.0;
				}
				else
				{
					num5 = num4 + num3 / 2.0;
				}
			}
			this.tbLevelOffset.Text = num5.ToString();
		}

		public void ButtonDuiqi(int index)
		{
			switch (index)
			{
			case 1:
				this.labelS0.Text = "水平:左对齐";
				this.labelS1.Text = "垂直:顶对齐";
				this.labelOffset.Text = " 顶部偏移量(mm):";
				this.btnDuiqi.Image = HYCreateDuct.Properties.Resources._1;
				this.tempType = 1;
				return;
			case 2:
				this.labelS0.Text = "水平:中心对齐";
				this.labelS1.Text = "垂直:顶对齐";
				this.labelOffset.Text = " 顶部偏移量(mm):";
				this.btnDuiqi.Image = HYCreateDuct.Properties.Resources._2;
				this.tempType = 2;
				return;
			case 3:
				this.labelS0.Text = "水平:右对齐";
				this.labelS1.Text = "垂直:顶对齐";
				this.labelOffset.Text = " 顶部偏移量(mm):";
				this.btnDuiqi.Image = HYCreateDuct.Properties.Resources._3;
				this.tempType = 3;
				return;
			case 4:
				this.labelS0.Text = "水平:左对齐";
				this.labelS1.Text = "垂直:中心对齐";
				this.labelOffset.Text = " 中心偏移量(mm):";
				this.btnDuiqi.Image = HYCreateDuct.Properties.Resources._4;
				this.tempType = 4;
				return;
			case 5:
				this.labelS0.Text = "水平:中心对齐";
				this.labelS1.Text = "垂直:中心对齐";
				this.labelOffset.Text = " 中心偏移量(mm):";
				this.btnDuiqi.Image = HYCreateDuct.Properties.Resources._5;
				this.tempType = 5;
				return;
			case 6:
				this.labelS0.Text = "水平:右对齐";
				this.labelS1.Text = "垂直:中心对齐";
				this.labelOffset.Text = " 中心偏移量(mm):";
				this.btnDuiqi.Image = HYCreateDuct.Properties.Resources._6;
				this.tempType = 6;
				return;
			case 7:
				this.labelS0.Text = "水平:左对齐";
				this.labelS1.Text = "垂直:底对齐";
				this.labelOffset.Text = " 底部偏移量(mm):";
				this.btnDuiqi.Image = HYCreateDuct.Properties.Resources._7;
				this.tempType = 7;
				return;
			case 8:
				this.labelS0.Text = "水平:中心对齐";
				this.labelS1.Text = "垂直:底对齐";
				this.labelOffset.Text = " 底部偏移量(mm):";
				this.btnDuiqi.Image = HYCreateDuct.Properties.Resources._8;
				this.tempType = 8;
				return;
			case 9:
				this.labelS0.Text = "水平:右对齐";
				this.labelS1.Text = "垂直:底对齐";
				this.labelOffset.Text = " 底部偏移量(mm):";
				this.btnDuiqi.Image = HYCreateDuct.Properties.Resources._9;
				this.tempType = 9;
				return;
			default:
				return;
			}
		}

		private void rbxdLevel_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbxdLevel.Checked)
			{
				this.cbStartLevel.Enabled = true;
				this.cbEndLevel.Enabled = true;
			}
		}

		private void rbjdLevel_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbjdLevel.Checked)
			{
				this.cbStartLevel.Enabled = false;
				this.cbEndLevel.Enabled = false;
			}
		}

		private List<int> GetDownOffset()
		{
			List<int> list = new List<int>();
			for (int i = 0; i < 16; i++)
			{
				list.Add(-400 + 50 * i);
			}
			return list;
		}

		private List<int> GetUpOffset()
		{
			List<int> list = new List<int>();
			for (int i = 0; i < 8; i++)
			{
				list.Add(2800 - 50 * i);
			}
			return list;
		}

		private void InitOffset(DuctHis hisHelpH)
		{
			this.cbStartOffset.Items.Clear();
			this.cbEndOffset.Items.Clear();
			List<int> downOffset = this.GetDownOffset();
			List<int> upOffset = this.GetUpOffset();
			foreach (int num in downOffset)
			{
				this.cbStartOffset.Items.Add(num);
			}
			this.cbStartOffset.SelectedIndex = 0;
			foreach (int num2 in upOffset)
			{
				this.cbEndOffset.Items.Add(num2);
			}
			this.cbEndOffset.SelectedIndex = 0;
			for (int i = 0; i < downOffset.Count; i++)
			{
				if (downOffset[i] == hisHelpH.nLevelSOffset)
				{
					this.cbStartOffset.SelectedIndex = i;
					break;
				}
			}
			if (this.cbStartOffset.Text != hisHelpH.nLevelSOffset.ToString())
			{
				this.cbStartOffset.Items.Add(hisHelpH.nLevelSOffset);
				this.cbStartOffset.SelectedIndex = this.cbStartOffset.Items.Count - 1;
			}
			for (int j = 0; j < upOffset.Count; j++)
			{
				if (upOffset[j] == hisHelpH.nLevelEOffset)
				{
					this.cbEndOffset.SelectedIndex = j;
					break;
				}
			}
			if (this.cbEndOffset.Text != hisHelpH.nLevelEOffset.ToString())
			{
				this.cbEndOffset.Items.Add(hisHelpH.nLevelEOffset);
				this.cbEndOffset.SelectedIndex = this.cbEndOffset.Items.Count - 1;
			}
		}

		private void FormCreateDuct_FormClosing(object sender, FormClosingEventArgs e)
		{
			try
			{
				DuctHisHelp ductHisHelp = new DuctHisHelp();
				DuctHis ductHis = new DuctHis();
				ductHis.nSystemType = this.cbSystemType.SelectedIndex;
				ductHis.nDuctType = this.cbDuctType.SelectedIndex;
				ductHis.nWindL = int.Parse(this.tbFlow.Text);
				if (this.txtWidth.Text == "0")
				{
					ductHis.nWidth = 120;
				}
				else
				{
					ductHis.nWidth = int.Parse(this.txtWidth.Text);
				}
				if (this.txtHeight.Text == "0")
				{
					ductHis.nHeight = 120;
				}
				else
				{
					ductHis.nHeight = int.Parse(this.txtHeight.Text);
				}
				ductHis.nLevel = this.cbLevel.SelectedIndex;
				ductHis.nOffset = int.Parse(this.tbLevelOffset.Text);
				ductHis.nType = this.Duiqi;
				ductHis.nTypeOffset = int.Parse(this.tbHorizontalOffset.Text);
				if (this.rbxdLevel.Checked)
				{
					ductHis.xdLevel = 0;
				}
				else
				{
					ductHis.xdLevel = 1;
				}
				ductHis.nLevelS = this.cbStartLevel.SelectedIndex;
				ductHis.nLevelSOffset = int.Parse(this.cbStartOffset.Text);
				ductHis.nLevelE = this.cbEndLevel.SelectedIndex;
				ductHis.nLevelEOffset = int.Parse(this.cbEndOffset.Text);
				ductHis.nFlow = int.Parse(this.tbFlow.Text);
				if (ductHis.nFlow <= 1)
				{
					ductHis.nFlow = 1000;
				}
				ductHis.nInner = this.nInner;
				ductHis.nTypeSelect = this.nTypeSelect;
				ductHisHelp.WriteHistory(ductHis);
			}
			catch
			{
			}
		}

		private void txtWidth_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '-' && this.txtWidth.Text != "")
			{
				MessageBox.Show("不能输入负数");
				this.txtWidth.Text = "120";
				e.Handled = true;
			}
		}

		private void txtHeight_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '-' && this.txtHeight.Text != "")
			{
				MessageBox.Show("不能输入负数");
				this.txtHeight.Text = "120";
				e.Handled = true;
			}
		}

		private void cbStartLevel_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.UpdateData();
		}

		private void cbEndLevel_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.UpdateData();
		}

		private void UpdateData()
		{
			if (this.title.Equals("绘制风管"))
			{
				return;
			}
			try
			{
				if (!this.cbStartOffset.Text.Equals("-") && !this.cbEndOffset.Text.Equals("-") && !string.IsNullOrEmpty(this.cbEndOffset.Text) && !string.IsNullOrEmpty(this.cbStartOffset.Text))
				{
					double num = double.Parse(this.cbStartOffset.Text) / 304.8;
					double num2 = double.Parse(this.cbEndOffset.Text) / 304.8;
					if (this.rbxdLevel.Checked)
					{
						num = this.zCoord(double.Parse(this.cbStartOffset.Text)) / 1000.0 + this.getLevelOffset(this.cbStartLevel.Text);
						num2 = this.zCoord(double.Parse(this.cbEndOffset.Text)) / 1000.0 + this.getLevelOffset(this.cbEndLevel.Text);
					}
					double num3 = Math.Abs(num2 - num);
					double num4 = double.Parse(this.lbRPa.Text.Replace("R(Pa/m) =", ""));
					num3 = num3 * 304.8 / 1000.0 * num4;
					this.lbPyPa.Text = "Py(Pa)=" + Math.Round(num3, 2);
				}
			}
			catch
			{
			}
		}

		private void cbStartOffset_TextChanged(object sender, EventArgs e)
		{
			try
			{
				if (this.cbStartOffset.Text.Equals("-"))
				{
					return;
				}
				double.Parse(this.cbStartOffset.Text);
			}
			catch
			{
				this.cbStartOffset.Text = "";
			}
			this.UpdateData();
		}

		private void cbEndOffset_TextChanged(object sender, EventArgs e)
		{
			try
			{
				if (this.cbEndOffset.Text.Equals("-"))
				{
					return;
				}
				double.Parse(this.cbEndOffset.Text);
			}
			catch
			{
				this.cbEndOffset.Text = "";
			}
			this.UpdateData();
		}

		private bool GetStatus()
		{
			if (this.title.Equals("绘制风管"))
			{
				if (string.IsNullOrEmpty(this.tbLevelOffset.Text))
				{
					this.tbLevelOffset.Text = "0";
					return false;
				}
				if (string.IsNullOrEmpty(this.tbHorizontalOffset.Text))
				{
					this.tbHorizontalOffset.Text = "0";
					return false;
				}
			}
			else
			{
				if (string.IsNullOrEmpty(this.cbStartOffset.Text))
				{
					this.cbStartOffset.Text = "0";
					return false;
				}
				if (string.IsNullOrEmpty(this.cbEndOffset.Text))
				{
					this.cbEndOffset.Text = "1000";
					return false;
				}
			}
			return true;
		}

		private UIApplication uiApp;

		private Document doc;

		private string title = "";

		private int Duiqi = 5;

		private int nInner;

		private int nTypeSelect;

		public bool blTiqvDuct;

		public List<DuctSytemSet> lstOutDuctSytemSet = new List<DuctSytemSet>();

		private int tempType = 5;
	}
}
